package homework;

public class BinarySearchTree {

    static class TreeNode {
        public int key;
        public TreeNode left;
        public TreeNode right;

        TreeNode(int key) {
            this.key = key;
        }
    }

    public TreeNode root;

    /**
     * 插入一个元素
     * @param key
     */
    public boolean insert(int key) {
        TreeNode node = new TreeNode(key);
        if(this.root == null){
            this.root = node;
            return true;
        }
        TreeNode cur = this.root;
        TreeNode prevCur = null;
        while(cur != null){
            if(cur.key > key){
                prevCur = cur;
                cur = cur.left;
            }else if(cur.key < key){
                prevCur = cur;
                cur = cur.right;
            }else{
                return false;
            }
        }
        if(prevCur.key > key){
            prevCur.left = node;
        }else{
            prevCur.right = node;
        }
        return true;
    }
    //查找key是否存在
    public TreeNode search(int key) {
        TreeNode cur = this.root;
        while(cur != null){
            if(cur.key > key){
                cur = cur.left;
            }else if(cur.key < key){
                cur = cur.right;
            }else{
                return cur;
            }
        }
        return null;
    }
    //删除key的值
    public boolean remove(int key) {
        TreeNode cur = this.root;
        TreeNode parent = null;
        //删除前要先查找这个key
        while(cur != null){
            if(cur.key > key){
                parent = cur;
                cur = cur.left;
            }else if(cur.key < key){
                parent = cur;
                cur = cur.right;
            }else{
                //找到了
                if(cur.left == null){
                    if(cur == root){
                        root = root.right;
                    }else if(cur == parent.left){
                        parent.left = cur.right;
                    }else{
                        parent.right = cur.right;
                    }
                }else if(cur.right == null){
                    if(cur == root){
                        root = root.left;
                    }else if(cur == parent.left){
                        parent.left = cur.left;
                    }else{
                        parent.right = cur.left;
                    }
                }else{
                    TreeNode targetParent = cur;
                    TreeNode target = cur.right;
                    while(target.left != null){
                        targetParent  = target;
                        target = target.left;
                    }
                    if(target == targetParent.left){
                        cur.key = target.key;
                        targetParent.left = target.right;
                    }else{
                        cur.key = target.key;
                        targetParent.right = target.right;
                    }
                }
                return true;
            }
        }
        return false;
    }
}
